package com.tianyang.yukuaifu.business.auth.view;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.facebook.drawee.view.SimpleDraweeView;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.business.auth.bean.BusinessOutput;
import com.tianyang.yukuaifu.business.auth.bean.CompanyBasicInput;
import com.tianyang.yukuaifu.business.auth.bean.LicenseRegOutput;
import com.tianyang.yukuaifu.business.auth.bean.SampleImageOutput;
import com.tianyang.yukuaifu.business.auth.contract.AuthConfig;
import com.tianyang.yukuaifu.common.cache.CacheManage;
import com.tianyang.yukuaifu.common.cache.CacheModel;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.common.utils.DateCheckPopListener;
import com.tianyang.yukuaifu.module.dialog.DateValidDialog;
import com.tianyang.yukuaifu.module.dialog.SampleImageDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.IDateValidListener;
import com.tianyang.yukuaifu.module.wheel.interfaces.IWheelDataChangeCallback;
import com.tianyang.yukuaifu.utils.SoftInputUtil;
import com.tianyang.yukuaifu.utils.ToastUtil;
import com.tianyang.yukuaifu.utils.VerifyUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Description：企业认证基本信息
 * Created by xyj on 2024/3/10
 */
public class AuthCompanyBasicView extends LinearLayout {

    @BindView(R.id.busLicImgSdv)
    SimpleDraweeView busLicImgSdv;
    @BindView(R.id.mchNameEt)
    EditText mchNameEt;
    @BindView(R.id.nameEt)
    EditText nameEt;
    @BindView(R.id.registeredCapitalEt)
    EditText registeredCapitalEt;
    @BindView(R.id.licenseNoEt)
    EditText licenseNoEt;
    @BindView(R.id.licenseStartTv)
    TextView licenseStartTv;
    @BindView(R.id.licenseEndTv)
    TextView licenseEndTv;
    @BindView(R.id.industryTypeOneTv)
    TextView industryTypeOneTv;
//    @BindView(R.id.industryAliPayTv)
//    TextView industryAliPayTv;
    @BindView(R.id.industryWechatTv)
    TextView industryWechatTv;
    @BindView(R.id.placeCodeTextTv)
    TextView placeCodeTextTv;
    @BindView(R.id.businessScopeTv)
    TextView businessScopeTv;
    @BindView(R.id.merchantAddressEt)
    EditText merchantAddressEt;
//    @BindView(R.id.emailEt)
//    EditText emailEt;
//    @BindView(R.id.remarkEt)
//    EditText remarkEt;
    @BindView(R.id.nextStepBasicTv)
    Button nextStepBasicTv;
    @BindView(R.id.industryWeChatCl)
    ConstraintLayout industryWeChatCl;
    @BindView(R.id.registeredCapitalRl)
    RelativeLayout registeredCapitalRl;

    private SampleImageDialog vSampleImageDialog;

    private DateValidDialog vDateValidDialog;

    private DateCheckPopListener mDateStartPopListener;

    private DateCheckPopListener mDateEndPopListener;

    private CompanyBasicInput mCompanyBasicInput;

    private SampleImageOutput mOutput;

    private String posChannel, mchType;

    public AuthCompanyBasicView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(getContext(), R.layout.view_auth_company_basic, this);
        ButterKnife.bind(this);
        mCompanyBasicInput = new CompanyBasicInput();
        mOutput = CacheManage.getInstance().getCache(CacheModel.SAMPLE_IMAGE);
    }

    public void setMchType(String mchType) {
        this.mchType = mchType;
        if (TextUtils.equals(AuthConfig.MCH_TYPE_COMPANY, mchType)) {
            registeredCapitalRl.setVisibility(VISIBLE);
        }
    }

    @OnClick({R.id.takeBusLicPicTv, R.id.licenseStartTv, R.id.licenseEndTv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.takeBusLicPicTv:
                if (mOutput != null) {
                    if (vSampleImageDialog == null) {
                        vSampleImageDialog = new SampleImageDialog(getContext());
                    }
                    vSampleImageDialog.showPictureDataView(mOutput.getLicensePic());
                    vSampleImageDialog.show();
                }
                break;
            case R.id.licenseStartTv:
                if (mDateStartPopListener == null) {
                    mDateStartPopListener = new DateCheckPopListener((Activity) getContext(), true);
                }
                mDateStartPopListener.handleRegionCheck(view, "请选择经营期限始", licenseStartTv, new IWheelDataChangeCallback() {
                    @Override
                    public void wheelDataChangeCallback(String wheelData) {
                        if (!TextUtils.isEmpty(wheelData)) {
                            mCompanyBasicInput.setLicenseStart(wheelData);
                        }
                    }
                });
                break;
            case R.id.licenseEndTv:
                if (vDateValidDialog == null) {
                    vDateValidDialog = new DateValidDialog(getContext());
                }
                vDateValidDialog.setListener(new IDateValidListener() {
                    @Override
                    public void onDateValid(boolean isLongTerm) {
                        if (isLongTerm) {
                            licenseEndTv.setText("长期");
                            mCompanyBasicInput.setLicenseEnd(CommonConfig.DATE_LONG_TERM);
                        } else {
                            view.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    showEndValidDialog(view);
                                }
                            }, 100);
                        }
                    }
                });
                vDateValidDialog.show();
                break;
        }
    }

    private void showEndValidDialog(View view) {
        if (mDateEndPopListener == null) {
            mDateEndPopListener = new DateCheckPopListener((Activity) getContext(), true);
        }
        mDateEndPopListener.handleRegionCheck(view, "请选择经营期限至", licenseEndTv, new IWheelDataChangeCallback() {
            @Override
            public void wheelDataChangeCallback(String wheelData) {
                if (!TextUtils.isEmpty(wheelData)) {
                    mCompanyBasicInput.setLicenseEnd(wheelData);
                }
            }
        });
    }

    public void onFeedBackView(CompanyBasicInput input, String posChannel) {
        this.posChannel = posChannel;
        if (TextUtils.equals(AuthConfig.POS_CHANNEL_FY, posChannel)) {
            industryWeChatCl.setVisibility(View.VISIBLE);
        } else {
            industryWeChatCl.setVisibility(View.GONE);
        }
        if (input != null) {
            mCompanyBasicInput = input;
            if (!TextUtils.isEmpty(mCompanyBasicInput.getLicensePic()))
                busLicImgSdv.setImageURI(Uri.parse(mCompanyBasicInput.getLicensePic()));
            mchNameEt.setText(VerifyUtil.checkString(mCompanyBasicInput.getMchName()));
            nameEt.setText(VerifyUtil.checkString(mCompanyBasicInput.getName()));
            registeredCapitalEt.setText(VerifyUtil.checkString(mCompanyBasicInput.getRegisteredCapital()));
            licenseNoEt.setText(VerifyUtil.checkString(mCompanyBasicInput.getLicenseNo()));
            licenseStartTv.setText(VerifyUtil.checkString(mCompanyBasicInput.getLicenseStart()));
            if (TextUtils.equals(CommonConfig.DATE_LONG_TERM, mCompanyBasicInput.getLicenseEnd())) {
                licenseEndTv.setText("长期");
            } else {
                licenseEndTv.setText(VerifyUtil.checkString(mCompanyBasicInput.getLicenseEnd()));
            }
            industryTypeOneTv.setText(VerifyUtil.checkString(mCompanyBasicInput.getIndustryTypeOne()));
//            if (!TextUtils.isEmpty(mCompanyBasicInput.getAliPayMcc())) {
//                String[] aliPayMcc = mCompanyBasicInput.getAliPayMcc().split("_");
//                industryAliPayTv.setText(aliPayMcc[0]);
//                mCompanyBasicInput.setIndustryAliPay(aliPayMcc[0]);
//            }
            if (!TextUtils.isEmpty(mCompanyBasicInput.getWechatMcc())) {
                String[] wechatMcc = mCompanyBasicInput.getWechatMcc().split("_");
                industryWechatTv.setText(wechatMcc[0]);
                mCompanyBasicInput.setIndustryWechat(wechatMcc[0]);
            }
            if (!TextUtils.isEmpty(mCompanyBasicInput.getPlaceProvinceName())) {
                String placeCodeText = VerifyUtil.checkString(mCompanyBasicInput.getPlaceProvinceName())
                        + " " + VerifyUtil.checkString(mCompanyBasicInput.getPlaceCityName())
                        + " " + VerifyUtil.checkString(mCompanyBasicInput.getPlaceAreaName());
                placeCodeTextTv.setText(placeCodeText);
            }
            businessScopeTv.setText(VerifyUtil.checkString(mCompanyBasicInput.getBusinessScope()));
            merchantAddressEt.setText(VerifyUtil.checkString(mCompanyBasicInput.getMerchantAddress()));
//            emailEt.setText(VerifyUtil.checkString(mCompanyBasicInput.getEmail()));
//            remarkEt.setText(VerifyUtil.checkString(mCompanyBasicInput.getRemark()));
        }
    }

    public void showLicenseRegView(LicenseRegOutput output) {
        if (output != null) {
            licenseNoEt.setText(VerifyUtil.checkString(output.getLicenseNo()));
            mCompanyBasicInput.setLicenseNo(output.getLicenseNo());
            licenseStartTv.setText(VerifyUtil.checkString(output.getLicenseStart()));
            mCompanyBasicInput.setLicenseStart(output.getLicenseStart());
            if (TextUtils.equals(CommonConfig.DATE_LONG_TERM, output.getLicenseEnd())) {
                licenseEndTv.setText("长期");
            } else {
                licenseEndTv.setText(VerifyUtil.checkString(output.getLicenseEnd()));
            }
            mCompanyBasicInput.setLicenseEnd(output.getLicenseEnd());
            merchantAddressEt.setText(VerifyUtil.checkString(output.getMerchantAddress()));
            mCompanyBasicInput.setMerchantAddress(output.getMerchantAddress());
            mchNameEt.setText(VerifyUtil.checkString(output.getMerchantName()));
            mCompanyBasicInput.setMchName(output.getMerchantName());
            businessScopeTv.setText(VerifyUtil.checkString(output.getBusinessScope()));
            mCompanyBasicInput.setBusinessScope(output.getBusinessScope());
        }
    }

    public void showBusinessView(BusinessOutput output, String mccType) {
        if (output != null) {
            switch (mccType) {
                case AuthConfig.MCC_TYPE_BUSINESS:
                    mCompanyBasicInput.setIndustryTypeOne(output.getMccName());
                    mCompanyBasicInput.setMcc(output.getMcc());
                    industryTypeOneTv.setText(VerifyUtil.checkString(output.getMccName()));
                    break;
                case AuthConfig.MCC_TYPE_ALI:
                    mCompanyBasicInput.setIndustryAliPay(output.getTypeThree());
                    mCompanyBasicInput.setAliPayMcc(output.getTypeThree() + "_" + output.getMccCode());
//                    industryAliPayTv.setText(VerifyUtil.checkString(output.getTypeThree()));
                    break;
                case AuthConfig.MCC_TYPE_WX:
                    mCompanyBasicInput.setIndustryWechat(output.getTypeThree());
                    mCompanyBasicInput.setWechatMcc(output.getTypeThree() + "_" + output.getMccId());
                    industryWechatTv.setText(VerifyUtil.checkString(output.getTypeThree()));
                    break;
            }
        }
    }

    public void showPhotoView(String urlStr, String photoType) {
        switch (photoType) {
            case CommonConfig.TYPE_LICENSEPIC:
                busLicImgSdv.setImageURI(Uri.parse(urlStr));
                mCompanyBasicInput.setLicensePic(urlStr);
                break;
        }
    }

    public void showProvCityAreaView(String provCityName, String wheelCode) {
        if (!TextUtils.isEmpty(provCityName)) {
            String[] provCity = provCityName.split(" ");
            mCompanyBasicInput.setPlaceProvinceName(provCity[0]);
            mCompanyBasicInput.setPlaceCityName(provCity[1]);
            if (provCity.length > 2)
                mCompanyBasicInput.setPlaceAreaName(provCity[2]);
            String[] wheelCodes = wheelCode.split(" ");
            mCompanyBasicInput.setPlaceProvinceCode(wheelCodes[0]);
            mCompanyBasicInput.setPlaceCityCode(wheelCodes[1]);
            if (wheelCodes.length > 2)
                mCompanyBasicInput.setPlaceAreaCode(wheelCodes[2]);
        }
    }

    public void showBusinessScopeView(String businessScope) {
        if (!TextUtils.isEmpty(businessScope)) {
            businessScopeTv.setText(businessScope);
            mCompanyBasicInput.setBusinessScope(businessScope);
        }
    }

    public CompanyBasicInput getCompanyBasicInput() {
        mCompanyBasicInput.setMchName(mchNameEt.getText().toString());
        mCompanyBasicInput.setName(nameEt.getText().toString());
        mCompanyBasicInput.setRegisteredCapital(registeredCapitalEt.getText().toString());
        mCompanyBasicInput.setLicenseNo(licenseNoEt.getText().toString());
        mCompanyBasicInput.setBusinessScope(businessScopeTv.getText().toString());
        mCompanyBasicInput.setMerchantAddress(merchantAddressEt.getText().toString());
//        mCompanyBasicInput.setEmail(emailEt.getText().toString());
//        mCompanyBasicInput.setRemark(remarkEt.getText().toString());
        return mCompanyBasicInput;
    }

    public boolean checkCompanyBasic() {
        if (TextUtils.isEmpty(mCompanyBasicInput.getLicensePic())) {
            ToastUtil.show("请上传营业执照");
            return false;
        }
        if (TextUtils.isEmpty(mchNameEt.getText())) {
            ToastUtil.show("请输入商户全称");
            SoftInputUtil.openKeybord(mchNameEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(nameEt.getText())) {
            ToastUtil.show("请输入商户简称");
            SoftInputUtil.openKeybord(nameEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(registeredCapitalEt.getText()) && TextUtils.equals(AuthConfig.MCH_TYPE_COMPANY, mchType)) {
            ToastUtil.show("请输入注册资金");
            SoftInputUtil.openKeybord(registeredCapitalEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(licenseNoEt.getText())) {
            ToastUtil.show("请输入统一信用代码");
            SoftInputUtil.openKeybord(licenseNoEt, getContext());
            return false;
        }
        if (TextUtils.isEmpty(licenseStartTv.getText())) {
            ToastUtil.show("请选择经营期起始");
            return false;
        }
        if (TextUtils.isEmpty(licenseEndTv.getText())) {
            ToastUtil.show("请选择经营期至");
            return false;
        }
        if (TextUtils.isEmpty(mCompanyBasicInput.getIndustryTypeOne())) {
            ToastUtil.show("请选择一级分类");
            return false;
        }
//        if (TextUtils.isEmpty(mCompanyBasicInput.getAliPayMcc())) {
//            ToastUtil.show("请选择支付宝行业");
//            return false;
//        }
        if (TextUtils.isEmpty(mCompanyBasicInput.getWechatMcc())
                && TextUtils.equals(AuthConfig.POS_CHANNEL_FY, posChannel)) {
            ToastUtil.show("请选择微信行业");
            return false;
        }
        if (TextUtils.isEmpty(mCompanyBasicInput.getPlaceProvinceCode())) {
            ToastUtil.show("请选择地区码");
            return false;
        }
        if (TextUtils.isEmpty(mCompanyBasicInput.getBusinessScope())) {
            ToastUtil.show("请选择经营范围");
            return false;
        }
//        if (TextUtils.isEmpty(businessScopeEt.getText())) {
//            ToastUtil.show("请填写经营范围");
//            SoftInputUtil.openKeybord(businessScopeEt, getContext());
//            return false;
//        }
        if (TextUtils.isEmpty(merchantAddressEt.getText())) {
            ToastUtil.show("请填写营业执照详细地址");
            SoftInputUtil.openKeybord(merchantAddressEt, getContext());
            return false;
        }
//        if (TextUtils.isEmpty(emailEt.getText())) {
//            ToastUtil.show("请输入电子邮箱");
//            SoftInputUtil.openKeybord(emailEt, getContext());
//            return false;
//        }
//        if (TextUtils.isEmpty(remarkEt.getText())) {
//            ToastUtil.show("请输入备注");
//            SoftInputUtil.openKeybord(remarkEt, getContext());
//            return false;
//        }
        return true;
    }

    public void setOnlyReadView() {
        mchNameEt.setEnabled(false);
        nameEt.setEnabled(false);
        registeredCapitalEt.setEnabled(false);
        licenseNoEt.setEnabled(false);
        licenseStartTv.setEnabled(false);
        licenseStartTv.setCompoundDrawables(null, null, null, null);
        licenseEndTv.setEnabled(false);
        licenseEndTv.setCompoundDrawables(null, null, null, null);
        industryTypeOneTv.setEnabled(false);
        industryTypeOneTv.setCompoundDrawables(null, null, null, null);
//        industryAliPayTv.setEnabled(false);
//        industryAliPayTv.setCompoundDrawables(null, null, null, null);
        industryWechatTv.setEnabled(false);
        industryWechatTv.setCompoundDrawables(null, null, null, null);
        placeCodeTextTv.setEnabled(false);
        placeCodeTextTv.setCompoundDrawables(null, null, null, null);
        businessScopeTv.setEnabled(false);
        merchantAddressEt.setEnabled(false);
//        emailEt.setEnabled(false);
//        remarkEt.setEnabled(false);
        nextStepBasicTv.setVisibility(GONE);
    }

    public void onDestroyView() {
        if (vSampleImageDialog != null && vSampleImageDialog.isShowing()) {
            vSampleImageDialog.dismiss();
            vSampleImageDialog = null;
        }
        if (vDateValidDialog != null) {
            vDateValidDialog.dismiss();
            vDateValidDialog = null;
        }
    }
}
